package com.test.service.impl;

import com.test.controller.Admininfo.MyObject;
import com.test.controller.Admininfo.ResCode;
import com.test.controller.Admininfo.ResData;
import com.test.entity.HrInfo;
import com.test.dao.HrInfoDao;
import com.test.service.HrInfoService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 人力资源(HrInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-11-12 11:01:47
 */
@Service("hrInfoService")
public class HrInfoServiceImpl implements HrInfoService {
    @Resource
    private HrInfoDao hrInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public HrInfo queryById(Integer id) {
        return this.hrInfoDao.queryById(id);
    }
    /**
     * 新增数据
     *
     * @param hrInfo 实例对象
     * @return 实例对象
     */
    @Override
    public HrInfo insert(HrInfo hrInfo) {
        this.hrInfoDao.insert(hrInfo);
        return hrInfo;
    }

    /**
     * 修改数据
     *
     * @param hrInfo 实例对象
     * @return 实例对象
     */
    @Override
    public Integer update(HrInfo hrInfo) {
        return this.hrInfoDao.update(hrInfo);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.hrInfoDao.deleteById(id) > 0;
    }

    @Override
    public HrInfo selectByHrInfo(String name, String password) {
        return hrInfoDao.selectByHrInfo(name,password);
    }


    @Override
    public ResData queryByPage(HrInfo hrInfo, PageRequest pageRequest) {
        int total = Integer.parseInt(String.valueOf(this.hrInfoDao.count(hrInfo)));

        List<HrInfo> hrInfosList = hrInfoDao.queryAllByLimit(hrInfo, pageRequest);

        int pageSize = pageRequest.getPageSize();

        int page=(total%pageSize==0)? total/pageSize:total/pageSize+1;

        List<MyObject> myObjectList = hrInfosList.stream().map(hrInfo1 -> new MyObject(hrInfo1)).collect(Collectors.toList());

        return new ResData(ResCode.SUCCESS,myObjectList,total,page);
    }


}
